package com.xuj.recyclerviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.xuj.recyclerviewdemo.adapter.GridItemDecoration;
import com.xuj.recyclerviewdemo.adapter.SimpleAdapter;
import com.xuj.recyclerviewdemo.adapter.OnItemClickListener;
import com.xuj.recyclerviewdemo.custom.ImageListAdapter;
import com.xuj.recyclerviewdemo.custom.ItemClickInterface;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends Activity {


    @BindView(R.id.id_recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.id_swiperefreshlayout)
    SwipeRefreshLayout mSwiperefreshlayout;
    private RecyclerView.LayoutManager mLayoutManager;
    //    private SimpleAdapter mSimpleAdapter;
//    private HeaderAdapter mHeaderAdapter;
//    private ImageListAdapter mImageListAdapter;

    private BSAdapter mBSAdapter;
    private List<String> mDatas;

    private View mHeaderView;
    private ItemClickInterface mItemClickInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        initData();
        initView();
        initEvent();

    }

    private void initEvent() {
    }

    private void initView() {

        mSwiperefreshlayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        mSwiperefreshlayout.setColorSchemeResources(android.R.color.holo_purple,
                android.R.color.holo_red_light, android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        mSwiperefreshlayout.setProgressViewOffset(false, 0, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
                        .getDisplayMetrics()));

        mSwiperefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mSwiperefreshlayout.setRefreshing(false);

                        Toast.makeText(MainActivity.this, "onRefresh", Toast.LENGTH_SHORT).show();
                    }
                }, 3000);
            }
        });
        mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mHeaderView = LayoutInflater.from(this).inflate(R.layout.item_header, null, false);
//      mLayoutManager = new GridLayoutManager(this, 2);
//      mLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.addItemDecoration(new GridItemDecoration(this, true));
//        mHeaderAdapter=new HeaderAdapter(this,mDatas);
//        mHeaderAdapter.addDatas(mDatas);
//        mRecyclerView.setAdapter(mHeaderAdapter);


//        mSimpleAdapter = new SimpleAdapter(this);
//        mSimpleAdapter.addDatas(mDatas);
//        mRecyclerView.setAdapter(mSimpleAdapter);
//        mSimpleAdapter.setOnItemClickListener(new OnItemClickListener() {
//            @Override
//            public void onItemClick(View view, int position) {
//
//            }
//
//            @Override
//            public void onItemLongClick(View view, int position) {
//
//            }
//        });

        mBSAdapter = new BSAdapter(this);
        mBSAdapter.addData(mDatas);
        mRecyclerView.setAdapter(mBSAdapter);
        setHeader(mRecyclerView);
        mBSAdapter.setOnItemClickListener(new BSAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position, String data) {
                Toast.makeText(MainActivity.this, "Click " + position + "data" + data, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onItemLongClick(View view, int position, String data) {
                Toast.makeText(MainActivity.this, "LongClick " + position + "data" + data, Toast.LENGTH_SHORT).show();

            }
        });

//        mImageListAdapter = new ImageListAdapter(this, mDatas, mItemClickInterface);
//        mRecyclerView.setAdapter(mImageListAdapter);
//        mImageListAdapter.addHeaderView(mHeaderView);
    }

    private void initData() {
        mDatas = new ArrayList<String>();
        for (int i = 'A'; i < 'Z'; i++) {
            mDatas.add("" + (char) i);
        }

    }

    private void setHeader(RecyclerView view) {
        View header = LayoutInflater.from(this).inflate(R.layout.item_header, view, false);
        mBSAdapter.addHeaderView(header);
    }


}
